using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Infragistics.UltraChart.Resources.Appearance;
using Infragistics.UltraChart.Shared.Styles;

namespace CustomChart
{
	public class TimeChart : BaseChart
	{
		private const string KeyLabelHashTime = "Time Axis";

		protected override void CustomAxisX()
		{
			_Chart.LabelHash.Add("Time Axis", new TimeAxisRender());
			AxisX.Labels.ItemFormat = AxisItemLabelFormat.Custom;
			AxisX.Labels.ItemFormatString = "<Time Axis>";
			AxisX.TickmarkStyle = AxisTickStyle.Smart;
		}

		public new void SetXFormat(string format)
		{
		}

		public override string GetXFormat()
		{
			return AxisX.Labels.ItemFormatString.Replace("<Time Axis>", "");
		}

		protected override string FormatX(double value)
		{
			if (value < 0.0)
			{
				value = 0.0;
			}
			return new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(value).ToLongTimeString();
		}

		protected override string GetDefaultTooltips(XYDataPoint data)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.AppendLine(_Chart.TitleBottom.Text + ":" + FormatX(data.ValueX));
			stringBuilder.AppendLine(_Chart.TitleLeft.Text + ":" + data.ValueY.ToString(GetFormat(AxisY.Labels)));
			return stringBuilder.ToString();
		}

		protected override void CustomMenu()
		{
			ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("axis_zoom");
			toolStripMenuItem.Click += base.Zoom_Click;
			ContentMenu.Items.Insert(0, toolStripMenuItem);
		}

		protected override void menu_Opening(object sender, CancelEventArgs e)
		{
			base.menu_Opening(sender, e);
			foreach (ToolStripItem item in ContentMenu.Items)
			{
				if (!(item is ToolStripMenuItem))
				{
					continue;
				}
				ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)item;
				if (toolStripMenuItem.Text == Utility.GetMessage("copy_curve_data"))
				{
					toolStripMenuItem.Visible = base.SelectedSerie != null && DataLineLayerList.Where((ChartLayerAppearance s) => s.Series.Contains(base.SelectedSerie)).Count() == 1;
				}
			}
		}

		protected override void CopyCurveData_Click(object sender, EventArgs e)
		{
			if (base.SelectedSerie == null)
			{
				return;
			}
			XYSeriesEx xYSeriesEx = base.SelectedSerie as XYSeriesEx;
			if (xYSeriesEx == null)
			{
				return;
			}
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < xYSeriesEx.Points.Count; i++)
			{
				DateTime dateTime = new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(xYSeriesEx.Points[i].ValueX);
				stringBuilder.Append(string.Format("{0}:{1}:{2}.{3}", dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond));
				if (i != xYSeriesEx.Points.Count - 1)
				{
					stringBuilder.Append("\t");
				}
			}
			stringBuilder.AppendLine(string.Empty);
			for (int j = 0; j < xYSeriesEx.Points.Count; j++)
			{
				stringBuilder.Append(xYSeriesEx.Points[j].ValueY);
				if (j != xYSeriesEx.Points.Count - 1)
				{
					stringBuilder.Append("\t");
				}
			}
			SLANClipboard.SetToClipboard(stringBuilder.ToString());
		}
	}
}
